package com.cskaoyan.exception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author stone
 * @date 2022/12/26 17:26
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice //上面两个注解的和，意味着里面的全部方法响应的都是字符串或Json字符串
public class CustomExceptionControllerAdvice {

    /*@ExceptionHandler(ArithmeticException.class)
    public ModelAndView resolveArithmeticException(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/exception.jsp");
        return modelAndView;
    }*/
    /*@ExceptionHandler(ArithmeticException.class)
    public String resolveArithmeticException(){

        return "/exception.jsp";//返回值作为ModelAndView中的视图名
    }*/

    @ExceptionHandler(ArithmeticException.class)
    //@ResponseBody
    public BaseRespVo resolveArithmeticException(ArithmeticException exception){
        String message = exception.getMessage();//by zero
        return BaseRespVo.fail("算术异常！" + message);
    }
}
